﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using System.IO;
using System.Collections.ObjectModel;

namespace Navigant.Infrastructure.Helpers
{
    public static class EmailHelper
    {
        public static void SendMessage(string to, string from,
            string subject, string body)
        {
            EmailHelper.SendMessage(to, from, subject, body, null, null, null);
        }

        public static void SendMessage(string to, string from,
            string subject, string body, 
            ReadOnlyCollection<DomainBase.Attachment> attachments)
        {
            EmailHelper.SendMessage(to, from, subject, body, null, attachments, null);
        }

        public static void SendMessage(string to, string from,
            string subject, string body, 
            ReadOnlyCollection<DomainBase.HtmlImageData> embeddedImages)
        {
            EmailHelper.SendMessage(to, from, subject, body, null, null, embeddedImages);
        }

        public static void SendMessage(string to, string from,
            string subject, string body, IList<DomainBase.Attachment> attachments, 
            IList<DomainBase.HtmlImageData> embeddedImages)
        {
            EmailHelper.SendMessage(to, from, subject, body, null, attachments, embeddedImages);
        }

        public static void SendMessage(string to, string from,
            string subject, string body, string smtpServer)
        {
            EmailHelper.SendMessage(to, from, subject, body, smtpServer, null, null);
        }

        public static void SendMessage(string to, string from,
            string subject, string body, string smtpServer,
            IList<DomainBase.Attachment> attachments)
        {
            EmailHelper.SendMessage(to, from, subject, body, smtpServer, null, null);
        }

        public static void SendMessage(string to, string from,
            string subject, string body, string smtpServer,
            IList<DomainBase.Attachment> attachments, 
            IList<DomainBase.HtmlImageData> embeddedImages)
        {
            if (string.IsNullOrEmpty(from))
            {
                from = ConfigurationManager.AppSettings["fromAddress"];
            }
            using (MailMessage message = new MailMessage())
            {
                string[] parsedToLine = to.Split(';');
                foreach (string parsedTo in parsedToLine)
                {
                    message.To.Add(new MailAddress(parsedTo));
                }
                message.From = new MailAddress(from);
                message.Subject = subject;
                if (embeddedImages != null && embeddedImages.Count > 0)
                {
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    LinkedResource embeddedImage = null;
                    MemoryStream contentStream = null;
                    foreach (DomainBase.HtmlImageData image in embeddedImages)
                    {
                        contentStream = new MemoryStream();
                        contentStream.Write(image.RawData, 0, image.RawData.Length);
                        contentStream.Position = 0;
                        embeddedImage = new LinkedResource(contentStream);
                        embeddedImage.ContentId = image.Name;
                        htmlView.LinkedResources.Add(embeddedImage);
                    }
                    message.AlternateViews.Add(htmlView);
                }
                else
                {
                    message.IsBodyHtml = true;
                    message.Body = body;
                }
                if (attachments != null && attachments.Count > 0)
                {
                    MemoryStream stream = null;
                    foreach (DomainBase.Attachment attachment in attachments)
                    {
                        stream = new MemoryStream();
                        stream.Write(attachment.Content, 0, attachment.Content.Length);
                        stream.Position = 0;
                        message.Attachments.Add(new Attachment(stream, attachment.FileName));
                    }
                }
                EmailHelper.SendMessage(message, smtpServer);
            }
        }

        private static void SendMessage(MailMessage message, string smtpServer)
        {
            if (string.IsNullOrEmpty(smtpServer))
            {
                smtpServer = ConfigurationManager.AppSettings["smtpServer"];
            }
            SmtpClient client = new SmtpClient(smtpServer);
            client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            client.Send(message);
        }

        public static void SendMessage(MailMessage message)
        {
            EmailHelper.SendMessage(message, null);
        }
    }
}
